// StudentList.cpp
#include "StudentList.h"
#include <iostream>
using namespace std;

// StudentNode 
StudentNode::StudentNode(int id, string name, int age, string major)
    : id(id), name(name), age(age), major(major), next(nullptr) {
}

// StudentLinkedList 构造函数实现
StudentLinkedList::StudentLinkedList() : head(nullptr), tail(nullptr), length(0) {}

// StudentLinkedList 析构函数实现
StudentLinkedList::~StudentLinkedList() {
    StudentNode* current = head;
    while (current != nullptr) {
        StudentNode* temp = current;
        current = current->next;
        delete temp;
    }
    head = tail = nullptr;
    length = 0;
}

// 头插法实现
void StudentLinkedList::insertAtHead(int id, string name, int age, string major) {
    StudentNode* newNode = new StudentNode(id, name, age, major);
    if (length == 0) {
        head = tail = newNode;
    }
    else {
        newNode->next = head;
        head = newNode;
    }
    length++;
}

// 尾插法实现
void StudentLinkedList::insertAtTail(int id, string name, int age, string major) {
    StudentNode* newNode = new StudentNode(id, name, age, major);
    if (length == 0) {
        head = tail = newNode;
    }
    else {
        tail->next = newNode;
        tail = newNode;
    }
    length++;
}

// 按ID删除实现
bool StudentLinkedList::deleteByID(int id) {
    if (length == 0) {
        cout << "链表为空，无法删除！" << endl;
        return false;
    }
    StudentNode* current = head;
    StudentNode* prev = nullptr;
    while (current != nullptr && current->id != id) {
        prev = current;
        current = current->next;
    }
    if (current == nullptr) {
        cout << "未找到学号为 " << id << " 的学生！" << endl;
        return false;
    }
    if (prev == nullptr) {
        head = current->next;
        if (head == nullptr) tail = nullptr;
    }
    else if (current == tail) {
        prev->next = nullptr;
        tail = prev;
    }
    else {
        prev->next = current->next;
    }
    delete current;
    length--;
    cout << "学号为 " << id << " 的学生已删除！" << endl;
    return true;
}

// 按ID查找实现
StudentNode* StudentLinkedList::findByID(int id) {
    StudentNode* current = head;
    while (current != nullptr) {
        if (current->id == id) return current;
        current = current->next;
    }
    return nullptr;
}

// 显示所有学生实现
void StudentLinkedList::displayAll() {
    if (length == 0) {
        cout << "链表中没有学生信息！" << endl;
        return;
    }
    StudentNode* current = head;
    cout << "===== 学生信息列表（共 " << length << " 人） =====" << endl;
    while (current != nullptr) {
        cout << "学号: " << current->id << endl;
        cout << "姓名: " << current->name << endl;
        cout << "年龄: " << current->age << endl;
        cout << "专业: " << current->major << endl;
        cout << "------------------------" << endl;
        current = current->next;
    }
}

// 新增：保存信息到文件（外部文件读写核心逻辑）
void StudentLinkedList::saveToFile(const string& filename) {
    ofstream outFile(filename);
    if (!outFile) { cout << "文件打开失败！" << endl; return; }
    StudentNode* current = head;
    while (current) {
        outFile << current->id << " " << current->name << " " << current->age << " " << current->major << "\n";
        current = current->next;
    }
    outFile.close(); cout << "信息已保存到" << filename << "！" << endl;
}

// 新增：从文件加载信息（外部文件读写核心逻辑）
void StudentLinkedList::loadFromFile(const string& filename) {
    ifstream inFile(filename);
    if (!inFile) { cout << "文件不存在或打开失败！" << endl; return; }
    // 先清空原有链表，避免重复
    while (head) { StudentNode* temp = head; head = head->next; delete temp; }
    tail = nullptr; length = 0;
    // 读取文件内容并添加到链表
    int id, age; string name, major;
    while (inFile >> id >> name >> age >> major) { insertAtTail(id, name, age, major); }
    inFile.close(); cout << "已从" << filename << "加载信息！" << endl;
}

// 获取长度实现
int StudentLinkedList::getLength() const {
    return length;
}

// 获取头节点实现
StudentNode* StudentLinkedList::getHead() const {
    return head;
}

// 获取尾节点实现
StudentNode* StudentLinkedList::getTail() const {
    return tail;
}